package org.shangrila.bme.mb;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.bme.eo.BmePurchDetail;
import org.shangrila.bme.eo.BmePurchOrder;
import org.shangrila.bme.eo.BmeSupplier;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.comm.helper.DateHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Named
@SessionScoped
public class PurchQueryMB implements Serializable {
	private static final long serialVersionUID = -59533921213217112L;
	private final Logger log = LoggerFactory.getLogger(this.getClass());

    private BmePurchOrder io = new BmePurchOrder();
	private List<BmePurchOrder> voc = new ArrayList<BmePurchOrder>(); 
	private List<BmePurchDetail> vod = new ArrayList<BmePurchDetail>(); //详情
	private BmePurchOrder selectedd = new BmePurchOrder();//选择详情信息

	//获取相关数据字典
	private List<SelectItem> itemsPurchStatus = AppInitInfo.getDropdownList("PurchStatus","null"); 

	private Date beginTime;	//起始时间
	private Date endTime;		//结束时间

	@Inject
	private DbAccess db;

	@PostConstruct
    public void init() {
	    BmeSupplier s = new BmeSupplier();
	    io.setBmeSupplier(s);
	
    }
         
    //查询信息
	public String queryAction() {	
		StringBuilder sql = new StringBuilder("SELECT m FROM BmePurchOrder m ");
		StringBuilder where = new StringBuilder(" WHERE ");
		String para = "";
		
		if(null!=io.getId() && !"".equals(io.getId())) {
			where.append("m.id=?1");
			para = io.getId();
		}
		else if(null!=io.getContName() && !"".equals(io.getContName())) {
			where.append("m.contName like ?1");
			para = io.getContName()+"%";
		}
		else if(null!=io.getBmeSupplier().getName() && !"".equals(io.getBmeSupplier().getName())) {
			where.append("m.bmeSupplier.name like ?1");
			para = io.getBmeSupplier().getName()+"%";
		}
		else if(null!=io.getConsignee() && !"".equals(io.getConsignee())) {
			where.append("m.consignee=?1");
			para = io.getConsignee();
		}
		else if(null!=io.getStatus() && !"".equals(io.getStatus())) {
			where.append("m.status=?1");
			para = io.getStatus();
		}
		else if(null != beginTime && null != endTime ) {
			where.append("m.orderDate BETWEEN ?1 AND ?2 ");
			String sTime = DateHelper.dateToStr(beginTime, DateHelper.DF_YYYY_MM_DD)+" 00:00:00";
			String eTime = DateHelper.dateToStr(endTime, DateHelper.DF_YYYY_MM_DD)+" 23:59:59";
			beginTime = DateHelper.strToDate(sTime, DateHelper.DF_DEFAULT);
			endTime  = DateHelper.strToDate(eTime, DateHelper.DF_DEFAULT);
		}
		
		else {
			where = new StringBuilder("");
		}
		
		log.info("queryAction..." + sql);

		if(where.toString().equals(""))		
			voc = db.findBySql(sql.toString());
		else {
			sql.append(where);
			if(sql.toString().indexOf("BETWEEN")>0)
				voc = db.findBySql(sql.toString(),new Object[]{beginTime,endTime});
			else
				voc = db.findBySql(sql.toString(),new Object[]{para});
		}
		if(voc.size()>0) {
			selectedd = voc.get(0);
			vod =  selectedd.getBmePurchDetails();
		}
		return null;
	}
	
    public void onPurchSelected(SelectEvent event) {
    	selectedd = (BmePurchOrder)event.getObject();
    	log.info("selectedd..." + selectedd);
    	vod = selectedd.getBmePurchDetails();   	
    }
       
	//判断BigDecimal为null
	public static boolean isEmpty(BigDecimal b) {
		return b == null ;
	}

    public BmePurchOrder getIo() {
		return io;
	}

	public void setIo(BmePurchOrder io) {
		this.io = io;
	}

	public List<BmePurchOrder> getVoc() {
		return voc;
	}

	public void setVoc(List<BmePurchOrder> voc) {
		this.voc = voc;
	}

	public List<SelectItem> getItemsPurchStatus() {
		return itemsPurchStatus;
	}

	public void setItemsPurchStatus(List<SelectItem> itemsPurchStatus) {
		this.itemsPurchStatus = itemsPurchStatus;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public List<BmePurchDetail> getVod() {
		return vod;
	}

	public void setVod(List<BmePurchDetail> vod) {
		this.vod = vod;
	}

	public BmePurchOrder getSelectedd() {
		return selectedd;
	}

	public void setSelectedd(BmePurchOrder selectedd) {
		this.selectedd = selectedd;
	}

}